package com.iman.common.util;

import java.net.InetAddress;
import java.util.Enumeration;

import javax.servlet.http.HttpServletRequest;

/**
 * 
 * @date 20100323
 */
public abstract class NetworkUtils {

	/**
	 * @param request
	 * @return such as: http://localhost:8080/sescportal/
	 */
	public static final String getBasePath(HttpServletRequest request) {
		String path = request.getContextPath();
		Enumeration e =  request.getHeaderNames();
		while(e.hasMoreElements()){
			String name =(String)e.nextElement();
			/**
			 * 一定会存在
			 */
			if("host".equalsIgnoreCase(name)){
				  String origin 	=request.getHeader(name);
				  if(origin!=null && !"".equals(origin)){
					  return request.getScheme() + "://" +origin+"/";
				  }
				}
			/*******
			 * 这个并不是一定总是有 我只是在第一次测试发现这个的  但是晚上我再次测试发现没有了
			 * 非集群的情况下是没有的
			 *  360浏览器的头信息可能就没有此项  根据浏览器不同
			 * 这个应该废弃
			 * 
			if("origin".equalsIgnoreCase(name)){
			  String origin 	=request.getHeader(name);
			  if(origin!=null && !"".equals(origin)){
				  return origin;
			  }
			}*******/
			/**
			 * 非集群情况没有
			 * 这个集群一定会用，但是这个的路径会很长 并不完全是域名 这个需要 自己找出域名才行
			 * 这个如果 不自己找好了的话 就废弃
			   360浏览器的头信息可能就没有此项    根据浏览器不同
			if("referer".equalsIgnoreCase(name)){
				  String origin 	=request.getHeader(name);
				  if(origin!=null && !"".equals(origin)){
					  return origin;
				  }
				}
			 */
		}
		request.getRequestURI();
		String basePath = request.getScheme() + "://" + request.getServerName()	+ ":" + request.getServerPort() + path + "/";
		return basePath;
	};

	/**
	 * 获取访问的客户端的真实的ip地址
	 * 
	 * @param request
	 *            HttpServletRequest
	 * @return
	 */
	public static String getIpAddr(HttpServletRequest request) {
		String ip = request.getHeader("x-forwarded-for");
		if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {
			ip = request.getHeader("Proxy-Client-IP");
		}
		if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {
			ip = request.getHeader("WL-Proxy-Client-IP");
		}
		if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {
			ip = request.getRemoteAddr();
		}
		return ip;
	};

	public static String getAllIpAddr(HttpServletRequest request) {
		StringBuffer buffer = new StringBuffer();
		buffer.append("x-forwarded-for:")
				.append(request.getHeader("x-forwarded-for")).append(";");
		buffer.append("Proxy-Client-IP:")
				.append(request.getHeader("Proxy-Client-IP")).append(";");
		buffer.append("WL-Proxy-Client-IP:")
				.append(request.getHeader("WL-Proxy-Client-IP")).append(";");
		buffer.append("IP:").append(request.getRemoteAddr()).append(";");
		return buffer.toString();
	};

	// 得到服务器IP
	public static String getServerIP() {
		try {
			InetAddress inet = InetAddress.getLocalHost();
			String hostip = inet.getHostAddress();
			return hostip;
		} catch (Exception e) {
			e.printStackTrace();
		}
		return "";
	};

	// 得到服务器IP
	public static String getServerName() {
		try {
		    InetAddress inet = InetAddress.getLocalHost();
		    String hostip = inet.getHostAddress();
		    String hostname = inet.getHostName();
		    System.out.println(hostip);
		    if("cp-was1".equals(hostname)){
		        return "214";
		    }
		    if("cp-was2".equals(hostname)){
                return "215";
            }
		    return "dev";
			//String hostname = inet.getHostName();
			//return hostname;
		} catch (Exception e) {
			e.printStackTrace();
		}
		return "";
	};
}
